package com.qinghe.pro.controller.main.article;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.article.Column;
import com.qinghe.pro.service.article.ColumnService;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;

@Controller
@RequestMapping("/manager/article/column")
public class ColumnController {

	@Autowired
	@Qualifier("columnServiceImpl")
	private ColumnService columnService;

	private static final String CREATE = "manager/article/column/create";
	private static final String UPDATE = "manager/article/column/update";
	private static final String VIEW = "manager/article/column/view";
	private static final String LIST = "manager/article/column/list";

	@RequiresPermissions("ArticleColumn:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
        map.put("columnList", columnService.findAllAsTree());
		return CREATE;
	}

	@RequiresPermissions("ArticleColumn:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody AjaxObject create(Column column) {
		columnService.insert(column);

		return AjaxObject.newOk("添加新闻栏目成功！");
	}

	@RequiresPermissions("ArticleColumn:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Integer id, Map<String, Object> map) {
		Column column = columnService.get(id);

		map.put("column", column);
        map.put("columnList", columnService.findAllAsTree());
		return UPDATE;
	}

	@RequiresPermissions("ArticleColumn:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody AjaxObject update(Column column) {
		columnService.update(column);

		return AjaxObject.newOk("修改新闻栏目成功！");
	}

	@RequiresPermissions("ArticleColumn:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody AjaxObject delete(@PathVariable Integer id) {
		columnService.delete(id);

		return AjaxObject.newOk("删除新闻栏目成功！");
	}

	@RequiresPermissions("ArticleColumn:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody AjaxObject deleteMany(@RequestParam("ids[]") List<Integer> ids) {
		for (Integer id : ids) {
			columnService.delete(id);
		}

		return AjaxObject.newOk("删除新闻栏目成功！");
	}

	@RequiresPermissions("ArticleColumn:view")
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		Column column = columnService.get(id);
		
		map.put("column", column);
		return VIEW;
	}

	@RequiresPermissions("ArticleColumn:show")
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Page page, Map<String, Object> map) {
        map.put("columnList", columnService.findAllAsTree());

		return LIST;
	}

}
